package com.example.day1.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.day1.common.CustomException;
import com.example.day1.common.R;
import com.example.day1.entity.Category;
import com.example.day1.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增数据成功");

    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        Page<Category> Page = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> QueryWrapper = new LambdaQueryWrapper<>();
        QueryWrapper.orderByAsc(Category::getSort);
     categoryService.page(Page, QueryWrapper);

        return R.success(Page);

    }

    /**
     * 根据用户id删除用户信息
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids) throws CustomException {
        categoryService.remove(ids);
        return R.success("分类信息删除成功");

    }

    /**
     * 修改分类信息
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category)
    {
        categoryService.updateById(category);
        return R.success("修改信息成功");

    }

    /**
     * 添加菜品页面，获取类型
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){

        LambdaQueryWrapper<Category> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        objectLambdaQueryWrapper.orderByAsc(Category::getSort);
        List<Category> list = categoryService.list(objectLambdaQueryWrapper);
        return R.success(list);
    }


}
